package com.lqian.service;

import com.lqian.annotation.RetryOnFailure;
import com.lqian.exception.RetryOnFailureException;
import com.lqian.pojo.Catalog;
import com.lqian.repository.CatalogRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

/**
 * @author：Melody_LQ
 * @time：2022/8/19 3:37 下午
 */
@Service
public class CatalogService {

    @Autowired
    private CatalogRepository catalogRepository;

    @Transactional(rollbackFor = Exception.class)
    @RetryOnFailure
    public boolean updateCatalogWithVersion(Long id,Long browseCount,Long version){
        boolean flag = false;
        if (catalogRepository.updateCatalogWithVersion(id,browseCount,version)>0){
            flag = true;
            System.out.println("hitting!!!");
        }else {
            throw new RetryOnFailureException("修改数据库失败，系统开始重试.....");
        }

        return flag;
    }

    public Optional<Catalog> findCatalogsForUpdate(Long id){
        return catalogRepository.findCatalogsForUpdate(id);
    }


}
